package com.yzf;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.FileProvider;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.hzl.Constants;
import com.hzl.ContactsActivity;
import com.hzl.DensityUtil;
import com.hzl.UserInfoBean;
import com.yao.component.circle.Constant;
import com.yao.component.circle.PhotoListAdapter2;
import com.yao.component.circle.R;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import com.yao.component.circle.dataobject.CircleProfile;
import com.yao.component.circle.dataobject.Post;
import com.yao.sdk.widget.ExtGridLayoutManager;
import com.yzf.bean.GroupBean;
public class PublishActivity extends Activity {

    private TextView shopNameTv;
    private ImageView locationTip;
    private TextView whoShowTv;
    private RecyclerView recyclerView;
    private PhotoListAdapter2 photoListAdapter;
    private ArrayList<String> picturePaths;
    private ArrayList<GroupBean> groupBeenList;
    private RecyclerView recycler_img;
    private ArrayList<UserInfoBean> imglist = new ArrayList<UserInfoBean>();
    private ArrayList<UserInfoBean> list = new ArrayList<UserInfoBean>();
    private ImageView img_remind;
    private ImageView whoShowTip;
    private EditText contentEditor;
    private LinearLayout img_item;
    private LinearLayout item_one;
    private LinearLayout item_two;
    private View inflate;
    private ImageView item_image;
    private int width;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish);
        initData();
        initView();
    }

    public void initData() {

        picturePaths = getIntent().getStringArrayListExtra("picture_list");
        if (picturePaths.size() < 9) {
            picturePaths.add("add_picture");
        }
    }

    String shopName;
    int postCodeType = 0;
    public static final String KEY_POST = "key_post";

    private Post readyForPost() {
        Post post = new Post();
        String content = contentEditor.getEditableText().toString();
        post.setContent(content);
        post.setAddress(shopName);
        post.setPraised(false);
        ArrayList<String> like_list = post.getLike_list();
        like_list.add("文杰");
        like_list.add("则飞");
        post.setLike_list(like_list);
        post.setUserId(CircleProfile.getInstance().getUserId());
        post.setPosterName(CircleProfile.getInstance().getName());
        post.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        int size = picturePaths.size();
        String s = picturePaths.get(size - 1);
        if ("add_picture".equals(s)) {
            picturePaths.remove(picturePaths.size() - 1);
        }
        post.setCirclePhoto(picturePaths);
        post.setRemindList(list);
        post.setCodeType(postCodeType);
        post.setGroupList(groupBeenList);
        return post;
    }

    private void initView() {

        contentEditor = (EditText) findViewById(R.id.publish_contant_et);

        findViewById(R.id.tv_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Post post = readyForPost();
                if (post != null) {
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(KEY_POST, post);
                    intent.putExtras(bundle);
                    setResult(Constant.REQUEST_CODE_POST, intent);
                    PublishActivity.this.finish();
                }
            }
        });

        findViewById(R.id.tv_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        recyclerView = (RecyclerView) findViewById(R.id.photo_list);
        photoListAdapter = new PhotoListAdapter2();
        photoListAdapter.setData(picturePaths);
        recyclerView.setLayoutManager(new ExtGridLayoutManager(this, 4));
        recyclerView.setAdapter(photoListAdapter);
        whoShowTv = (TextView) findViewById(R.id.whoshow_tv);
        whoShowTip = (ImageView) findViewById(R.id.img_show);
        View locationView = findViewById(R.id.location_view);
        locationView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = shopNameTv.getText().toString();
                if (s.contains("·")) {//过滤掉 xx市· 因为是自己添加的
                    String[] split = s.split("·");
                    String s1 = split[1];
                    Intent intent = new Intent(PublishActivity.this, ArroundAddressListActivity.class);
                    intent.putExtra("selectedName", s1);
                    startActivityForResult(intent, 1);
                } else {
                    Intent intent = new Intent(PublishActivity.this, ArroundAddressListActivity.class);
                    intent.putExtra("selectedName", s);
                    startActivityForResult(intent, 1);
                }

            }
        });
        View seeView = findViewById(R.id.whoshow_view);
        seeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = whoShowTv.getText().toString();
                Intent intent = new Intent(PublishActivity.this, ShowPermissionActivity.class);
                if ("公开".equals(s)) {
                    intent.putExtra("permissionType", 1);
                } else if ("私密".equals(s)) {
                    intent.putExtra("permissionType", 2);

                } else if (s.contains("除去")) {
                    intent.putExtra("permissionType", 4);//不给谁看

                } else {
                    intent.putExtra("permissionType", 3);//部分可见

                }
                intent.putExtra("permissionString", s);
                startActivityForResult(intent, 2);
            }
        });
        View remindView = findViewById(R.id.remind_view);
        remindView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PublishActivity.this, ContactsActivity.class);
                intent.putExtra("type", Constants.STARTREMIND);
                intent.putExtra("listimg", imglist);
                startActivityForResult(intent, Constants.CODE_FRIEND_LIST);
            }
        });
        shopNameTv = (TextView) findViewById(R.id.location_tv);
        locationTip = (ImageView) findViewById(R.id.location_tip);
        img_remind = (ImageView) findViewById(R.id.img_remind);
        img_item = (LinearLayout) findViewById(R.id.recycler_img);
        item_one = (LinearLayout) findViewById(R.id.item_one);
        item_two = (LinearLayout) findViewById(R.id.item_two);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            if (requestCode == 1 && resultCode == RESULT_OK) {
                shopName = data.getStringExtra("shopName");
                String cityName = data.getStringExtra("cityName");
                if ("不显示位置".equals(shopName)) {
                    shopNameTv.setText("所在位置");
                    locationTip.setBackgroundResource(R.drawable.location);
                } else {
                    if (!shopName.contains("市")) {
                        shopName = cityName + "·" + shopName;
                    }
                    shopNameTv.setText(shopName);
                    locationTip.setBackgroundResource(R.drawable.location_select);
                }
            } else if (requestCode == 2 && resultCode == Constants.SEND_NAME_CODE) {
                String shoShowPermission = data.getStringExtra("shoShowPermission");
                if ("公开".equals(shoShowPermission)) {
                    whoShowTip.setImageResource(R.drawable.who);
                    whoShowTv.setText("公开");
                    postCodeType = Post.CODE_PUBLIC;
                } else if ("私密".equals(shoShowPermission)) {
                    whoShowTip.setImageResource(R.drawable.who_select);
                    whoShowTv.setText("私密");
                    postCodeType = Post.CODE_PRIVATE;
                }
            } else if (requestCode == 2 && resultCode == Constants.SEND_BUFEN_CODE) {
                String shoShowPermission = data.getStringExtra("bufenGB");
                groupBeenList = (ArrayList<GroupBean>) data.getSerializableExtra("bufenGBlist");
                whoShowTv.setText(shoShowPermission);
                whoShowTip.setImageResource(R.drawable.who_select);
                postCodeType = Post.CODE_GROUP_VISIBLE;
            } else if (requestCode == 2 && resultCode == Constants.SEND_BUGEI_CODE) {
                String shoShowPermission = data.getStringExtra("bugeiGB");
                groupBeenList = (ArrayList<GroupBean>) data.getSerializableExtra("bugeiGBlist");
                whoShowTv.setText(shoShowPermission);
                whoShowTip.setImageResource(R.drawable.who_select);
                postCodeType = Post.CODE_GROUP_INVISIBLE;

            } else if (requestCode == Constants.CODE_FRIEND_LIST && resultCode == Constants.CODE_FRIEND_LIST) {
                list = (ArrayList<UserInfoBean>) data.getSerializableExtra("UserInfoBean");
                if (list != null) {
                    imglist.clear();
                    img_remind.setImageResource(R.drawable.remind_select);
                    imglist.addAll(list);
                    item_one.removeAllViews();
                    item_two.removeAllViews();
                    width = img_item.getWidth()/5;
                    for (int i = 0; i < imglist.size(); i++) {
                        inflate = LayoutInflater.from(this).inflate(R.layout.item_grid, null);
                        item_image = (ImageView) inflate.findViewById(R.id.item_img);
                        item_image.setLayoutParams(new LinearLayout.LayoutParams(width,width));
                        if (i>=0 && i<5){
                            item_image.setImageResource(imglist.get(i).getHeadImg());
                            item_one.addView(inflate);
                        }else if (i>=5){
                            item_image.setImageResource(imglist.get(i).getHeadImg());
                            item_two.addView(inflate);
                        }
                    }

                } else {
                    imglist.clear();
                    img_remind.setImageResource(R.drawable.remind);
                    item_one.removeAllViews();
                    item_two.removeAllViews();
                }
            }

        }
    }
}
